package com.erps.dao;

import java.util.List;
import java.util.Map;

import com.erps.CommonUtils.ActiveField;
import com.erps.CommonUtils.DateUtils;
import com.erps.CommonUtils.JActiveField;
import com.erps.CommonUtils.JActiveTable;
import com.erps.CommonUtils.Sequence;
import com.erps.CommonUtils.StringEx;
import com.erps.db.DBManager;
import com.erps.sys.rest.PageInfo;
import com.hmkj.entity.ErspResInfo;

public class ErspResInfoDao extends JActiveTable {
	public ErspResInfoDao() {
		setDBTableName("ersp_res_info");
		AddField(new JActiveField("id", "id", ActiveField.ftString, 36));
		setDefaultFieldValue("id", Sequence.getGuid());
		AddField(new JActiveField("org_id", "org_id", ActiveField.ftString, 36));
		AddField(new JActiveField("res_title", "res_title", ActiveField.ftString, 200));
		AddField(new JActiveField("res_contact", "res_contact", ActiveField.ftString, 200));
		AddField(new JActiveField("res_contact_phone", "res_contact_phone", ActiveField.ftString, 200));

		AddField(new JActiveField("res_summy", "res_summy", ActiveField.ftString, 200));
		AddField(new JActiveField("res_desc", "res_desc", ActiveField.ftString, Integer.MAX_VALUE));
		AddField(new JActiveField("res_release", "res_release", ActiveField.ftInteger, 4));
		AddField(new JActiveField("res_order_count", "res_order_count", ActiveField.ftInteger, 36));
		AddField(new JActiveField("res_use_count", "res_use_count", ActiveField.ftInteger, 11));
		AddField(new JActiveField("res_fav_count", "res_fav_count", ActiveField.ftInteger, 11));
		AddField(new JActiveField("res_star_count", "res_star_count", ActiveField.ftInteger, 11));
		AddField(new JActiveField("res_checked_user", "res_checked_user", ActiveField.ftString, 36));
		AddField(new JActiveField("res_checked", "res_checked", ActiveField.ftString, 2));
		AddField(new JActiveField("res_checked_response", "res_checked_response", ActiveField.ftString, 200));

		AddField(new JActiveField("res_type", "res_type", ActiveField.ftInteger, 11));
		AddField(new JActiveField("res_position", "res_position", ActiveField.ftString, 500));
		AddField(new JActiveField("create_time", "create_time", ActiveField.ftLong, 20));
		AddField(new JActiveField("res_posx", "res_posx", ActiveField.ftDouble, Integer.MAX_VALUE));
		AddField(new JActiveField("res_posy", "res_posy", ActiveField.ftDouble, Integer.MAX_VALUE));
		AddField(new JActiveField("res_syspath", "res_syspath", ActiveField.ftString, 200));

		AddField(new JActiveField("last_update_time", "last_update_time", ActiveField.ftLong));
		setDefaultFieldValue("last_update_time", String.valueOf(DateUtils.getDayTimeNum()));

		setDbKeyFields("id");

	}

	public int add(ErspResInfo vo) {
		setFieldValue("id", vo.getId());
		setFieldValue("org_id", vo.getOrg_id());
		setFieldValue("res_title", vo.getRes_title());
		setFieldValue("res_contact", vo.getRes_contact());
		setFieldValue("res_contact_phone", vo.getRes_contact_phone());
		setFieldValue("res_summy", vo.getRes_summy());
		setFieldValue("res_desc", vo.getRes_desc());
		setFieldValue("res_release", vo.getRes_release());
		setFieldValue("res_order_count", vo.getRes_order_count());
		setFieldValue("res_use_count", vo.getRes_use_count());
		setFieldValue("res_fav_count", vo.getRes_fav_count());
		setFieldValue("res_star_count", vo.getRes_star_count());
		setFieldValue("res_checked_user", vo.getRes_checked_user());
		setFieldValue("res_checked", vo.getRes_checked());
		setFieldValue("res_checked_response", vo.getRes_checked_response());
		setFieldValue("res_type", vo.getRes_type());
		setFieldValue("res_position", vo.getRes_position());
		setFieldValue("create_time", vo.getCreate_time());
		setFieldValue("res_posx", vo.getRes_posx());
		setFieldValue("res_posy", vo.getRes_posy());
		setFieldValue("res_syspath", vo.getRes_syspath());

		return DBManager.ExecSQL(this.getSQL());
	}

	public String getSQL(ErspResInfo vo) {
		setFieldValue("id", vo.getId());
		setFieldValue("org_id", vo.getOrg_id());
		setFieldValue("res_title", vo.getRes_title());
		setFieldValue("res_contact", vo.getRes_contact());
		setFieldValue("res_contact_phone", vo.getRes_contact_phone());
		setFieldValue("res_summy", vo.getRes_summy());
		setFieldValue("res_desc", vo.getRes_desc());
		setFieldValue("res_release", vo.getRes_release());
		setFieldValue("res_order_count", vo.getRes_order_count());
		setFieldValue("res_use_count", vo.getRes_use_count());
		setFieldValue("res_view_count", vo.getRes_view_count());
		setFieldValue("res_fav_count", vo.getRes_fav_count());
		setFieldValue("res_star_count", vo.getRes_star_count());
		setFieldValue("res_checked_user", vo.getRes_checked_user());
		setFieldValue("res_checked_response", vo.getRes_checked_response());
		setFieldValue("res_type", vo.getRes_type());
		setFieldValue("res_position", vo.getRes_position());
		setFieldValue("res_posx", vo.getRes_posx());
		setFieldValue("res_posy", vo.getRes_posy());
		setFieldValue("create_time", vo.getCreate_time());
		setFieldValue("res_syspath", vo.getRes_syspath());

		return this.getSQL();
	}

	public List<Map<String, String>> Query(PageInfo ActivePage, String cWhereParm) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) ");
		sql.append(" from " + this.getDBTableName());
		sql.append(" where " + cWhereParm);
		int iRSCount = StringEx.getInt(DBManager.GetStrValue(sql.toString()));

		String strSQL = "select id,res_id,res_title,res_summy,from " + this.getDBTableName() + " where " + cWhereParm;
		String vOrderBy = " last_update_time desc ";
		int iPageNO = ActivePage.getPageno();
		int iPageSize = ActivePage.getPagesize();
		DBManager.QueryData(strSQL, vOrderBy, iPageNO, iPageSize);
		return DBManager.QueryData(strSQL);
	}

	public int ChangeApproval(String cID, int iTypeID, String cCheckUser) {
		String cResult = (iTypeID == 1) ? "审核通过" : "未审核通过";
		String cResultID = (iTypeID == 1) ? "1" : "2";
		JActiveTable aTable = new JActiveTable();
		aTable.setDBTableName(this.getDBTableName());
		aTable.addFieldValue("id", cID);
		if (iTypeID == 1)
			aTable.addFieldValue("res_release", 1);
		aTable.addFieldValue("res_checked", cResultID);
		aTable.addFieldValue("res_checked_user", cCheckUser);
		aTable.addFieldValue("res_checked_response", cResult);
		aTable.set__rowkeys(cID);
		aTable.setDbKeyFields("id");

		return DBManager.ExecSQL(aTable.getSQL());
	}
}
